const adminUsersModel = require('../model/adminUsersModel');
module.exports = {
  adminDoLogin(req,res){
    // 获取浏览器发送的密码
    let {email,password} = req.body;
    // console.log(email,password);
    // 验证邮箱，密码是否正确
    // 找model获取用户名和密码
    // 返回结果
    adminUsersModel.validateEmailAndPassword(email,password,(err,result)=>{
      if(err) console.error(err);
      // console.log(result);
      let resObj = {
      code: 401,
      msg: '登陆失败',
      };
      if(result){
        resObj.code = 200;
        resObj.msg = '登陆成功';
        // 把登陆成功的信息写入session，然后在每个页面判断
        req.session.isLogin = true;
        req.session.userInfo = result; 
      };
      res.send(resObj);
    });
  },
  getNicknameImage(req,res){
  //  console.log(req.session);
   let {nickname,avatar} = req.session.userInfo;
   let resObj = {
     code: 401,
     msg: '获取失败',
   }
   if(req.session.userInfo){
     resObj.code = 200;
     resObj.msg = '获取成功';
     resObj.data = {
      nickname,avatar
     };
   };
   res.send(resObj);
  },
  adminLogout(req,res){
    req.session.isLogin = false;
    res.send({
      code: 200,
      msg: '退出成功',
    });
  }
};